package com.wannengbang.storemobile.utils;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;
import com.orhanobut.logger.Logger;
import com.wannengbang.storemobile.MyApplication;

/**
 * Time: 2019/7/26
 * Author: ZF
 * Description:
 * <p>
 * <p>
 * aMapLocation.getLocationType();//获取当前定位结果来源，如网络定位结果，详见定位类型表
 * aMapLocation.getLongitude();//获取经度
 * aMapLocation.getLatitude();//获取纬度
 * aMapLocation.getAccuracy();//获取精度信息
 * aMapLocation.getAddress();//地址，如果option中设置isNeedAddress为false，则没有此结果，网络定位结果中会有地址信息，GPS定位不返回地址信息。
 * aMapLocation.getCountry();//国家信息
 * aMapLocation.getProvince();//省信息
 * aMapLocation.getCity();//城市信息
 * aMapLocation.getDistrict();//城区信息
 * aMapLocation.getStreet();//街道信息
 * aMapLocation.getStreetNum();//街道门牌号信息
 * aMapLocation.getCityCode();//城市编码
 * aMapLocation.getAdCode();//地区编码
 * aMapLocation.getAoiName();//获取当前定位点的AOI信息
 * aMapLocation.getBuildingId();//获取当前室内定位的建筑物Id
 * aMapLocation.getFloor();//获取当前室内定位的楼层
 * aMapLocation.getGpsAccuracyStatus();//获取GPS的当前状态
 * //获取定位时间
 * SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
 * Date date = new Date(aMapLocation.getTime());
 * df.format(date);
 */

public class AMapManager {

    private AMapLocationClient mLocationClient = null;
    public AMapLocationClientOption mLocationOption = null;
    private OnLocationListener onLocationListener;

    public AMapManager() {
        try {
            mLocationClient = new AMapLocationClient(MyApplication.getContext());
        }catch (Exception e){
            
        }

    }


    public void getOnceLocation(OnLocationListener onLocationListener) {

        mLocationOption = new AMapLocationClientOption();
        //会同时使用网络定位和GPS定位，优先返回最高精度的定位结果，
        mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);

        mLocationOption.setOnceLocation(true);//获取一次定位结果：

        //设置是否返回地址信息（默认返回地址信息）
        mLocationOption.setNeedAddress(true);

        mLocationOption.setLocationCacheEnable(true);//开启缓存机制

        mLocationClient.setLocationOption(mLocationOption);//给定位客户端对象设置定位参数

        //获取定位结果
        mLocationClient.setLocationListener(new AMapLocationListener() {
            @Override
            public void onLocationChanged(AMapLocation aMapLocation) {
                Logger.i("onLocationChanged==");

                mLocationClient.stopLocation();

                if (aMapLocation.getErrorCode() == 0) {
                    SPManager.getInstance().saveLongitude(aMapLocation.getLongitude() + "");//获取经度
                    SPManager.getInstance().saveLatitude(aMapLocation.getLatitude() + "");//获取纬度
                }

                if (onLocationListener != null) {
                    onLocationListener.onLocationInfo(aMapLocation);
                }

            }
        });

        mLocationClient.startLocation(); //启动定位
    }


    public interface OnLocationListener {
        void onLocationInfo(AMapLocation aMapLocation);
    }


    public void onDestroy() {
        //销毁定位客户端
        if (mLocationClient != null) {
            mLocationClient.onDestroy();
            mLocationClient = null;
            mLocationOption = null;
        }
    }
}
